﻿using AccompanyCabinet.Helper;
using AccompanyCabinet.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AccompanyCabinet.Admin
{
    public partial class InComeDetail : Form
    {
        public InComeDetail()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 每页记录数(默认：10)
        /// </summary>
        public int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["pagesize"].ToString());
        public int ppsize = 0;
        /// <summary>
        /// 总记录数(默认：0)
        /// </summary>
        public int recordCount = 0;
        /// <summary>
        /// 总页数(默认：0)
        /// </summary>
        public int pageCount = 0;
        /// <summary>
        /// 当前页(默认：1)
        /// </summary>
        public int currentPage = 1;
        public int curpageIndex = 1;
        public string strss = "&startTime=" + DateTime.Now.ToString("yyyy-MM-dd") + "&endTime=" + DateTime.Now.Date.AddDays(1).ToString("yyyy-MM-dd") + "";
        public string status = "dev";
        private void InComeDetail_Load(object sender, EventArgs e)
        {
            if (status == "dev")
                Query_module();
            else QueryBox_module();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string startTime = dateTimePicker1.Text;
            string endTime = dateTimePicker2.Text;
            strss = "&startTime=" + startTime + "&endTime=" + endTime + "";
            Query_module();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string startTime = dateTimePicker1.Text;
            string endTime = dateTimePicker2.Text;
            strss = "&typeStatus=ZFB";
            Query_module();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string startTime = dateTimePicker1.Text;
            string endTime = dateTimePicker2.Text;
            strss = "&typeStatus=WX";
            Query_module();
        }
        public string BoxType(string status)
        {
            switch (status)
            {
                case "Big": return "大";
                case "Mid": return "中";
                case "Small": return "小";
                default: return "";
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.Hide();
            new Index().Show();
        }
        public void Query_module()
        {
            dgvincome.Columns.Clear();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("箱号", typeof(string)));
            dt.Columns.Add(new DataColumn("类型", typeof(string)));
            dt.Columns.Add(new DataColumn("总收入", typeof(string)));
            DataRow drs = null;
            drs = dt.NewRow();
            InComeModelpage inComes = new InComeModelpage();
            string url = "TerminalInfo/BoxIncomeDetail";
            if (currentPage == 1) ppsize = pageSize;
            if (inComes.PageCount == 0) pageSize = ppsize;
            var res = HttpHelper.Get(url + "?dId="+HttpHelper.deviceInfoModel.DId+"&pageIndex="+ curpageIndex + "&pageSize="+pageSize+"" + strss);
            if (res.status == "OK")
            {
                inComes = JsonConvert.DeserializeObject<InComeModelpage>(res.Message);
            }
            else
            {
                MessageBox.Show(res.Message);
                return;
            }
            pageCount = inComes.PageCount;
            currentPage = inComes.PageIndex;
            pageSize = inComes.PageSize;
            if (inComes.PageIndex == 1)
            {
                ppsize = pageSize;
            }
            if (pageCount == currentPage)
            {
                ppsize = pageSize;
                pageSize = inComes.TotalRows % pageSize==0? pageSize: inComes.TotalRows % pageSize;
            }
            lab_totalnum.Text = inComes.TotalRows.ToString();
            lab_totalpage.Text = inComes.PageCount.ToString();
            label_pagesize.Text = inComes.PageSize.ToString();
            txt_currentpage.Text = inComes.PageIndex.ToString() + "/" + inComes.PageCount.ToString();
            if(inComes.TotalRows != 0)
            {
                for (int i = 0; i < pageSize; i++)
                {
                    drs = dt.NewRow();
                    drs[0] = inComes.LsList[i].Num;
                    drs[1] = BoxType(inComes.LsList[i].TypeNo);
                    drs[2] = inComes.LsList[i].Price;
                    dt.Rows.Add(drs);
                }
            }
            
            dgvincome.DataSource = dt;
            dgvincome.AllowUserToAddRows = false;
        }

        public void QueryBox_module()
        {
            dgvincome.Columns.Clear();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("箱号", typeof(string)));
            dt.Columns.Add(new DataColumn("订单号", typeof(string)));
            dt.Columns.Add(new DataColumn("开始时间", typeof(string)));
            dt.Columns.Add(new DataColumn("结束时间", typeof(string)));
            dt.Columns.Add(new DataColumn("金额", typeof(string)));
            dt.Columns.Add(new DataColumn("时长", typeof(string)));
            dt.Columns.Add(new DataColumn("付款方式", typeof(string)));
            dt.Columns.Add(new DataColumn("是否超时", typeof(string)));
            DataRow drs = null;
            drs = dt.NewRow();
            BoxIncomeStpage inComes = new BoxIncomeStpage();
            string url = "TerminalInfo/BoxDetailIncome";
            if (curpageIndex == 1) ppsize = pageSize;
            if (inComes.PageCount == 0) pageSize = ppsize;
            var res = HttpHelper.Get(url + "?dId=" + HttpHelper.deviceInfoModel.DId + "&pageIndex=" + curpageIndex + "&pageSize=" + pageSize + ""+strss);
            if (res.status == "OK")
            {
                inComes = JsonConvert.DeserializeObject<BoxIncomeStpage>(res.Message);
            }
            else
            {
                MessageBox.Show(res.Message);
                return;
            }
            pageCount = inComes.PageCount;
            curpageIndex = inComes.PageIndex;
            pageSize = inComes.PageSize;
            if (inComes.PageIndex == 1)
            {
                ppsize = pageSize;
            }
            if (pageCount == curpageIndex)
            {
                ppsize = pageSize;
                pageSize = inComes.TotalRows % pageSize == 0 ? pageSize : inComes.TotalRows % pageSize;
            }
            lab_totalnum.Text = inComes.TotalRows.ToString();
            lab_totalpage.Text = inComes.PageCount.ToString();
            label_pagesize.Text = inComes.PageSize.ToString();
            txt_currentpage.Text = inComes.PageIndex.ToString() + "/" + inComes.PageCount.ToString();
            if (inComes.TotalRows != 0)
            {
                for (int i = 0; i < pageSize; i++)
                {
                    drs = dt.NewRow();
                    drs[0] = inComes.LsList[i].Num;
                    drs[1] = inComes.LsList[i].OrderNum;
                    drs[2] = inComes.LsList[i].StartTime.ToString("yyyy-MM-dd HH:mm");
                    drs[3] = inComes.LsList[i].EndTime?.ToString("yyyy-MM-dd HH:mm");
                    drs[4] = inComes.LsList[i].Price;
                    drs[5] = inComes.LsList[i].Duration;
                    drs[6] = inComes.LsList[i].Payment;
                    drs[7] = inComes.LsList[i].PayType;
                    dt.Rows.Add(drs);
                }
            }

            dgvincome.DataSource = dt;
            dgvincome.AllowUserToAddRows = false;
        }
        private void btn_Homepage_Click(object sender, EventArgs e)
        {
            curpageIndex = 1;
            pageSize = ppsize;
            if (status == "dev")
                Query_module();
            else QueryBox_module();
        }

        private void btn_prepage_Click(object sender, EventArgs e)
        {
            curpageIndex = curpageIndex - 1 > 0 ? curpageIndex - 1 : 1;
            if (curpageIndex + 1 >= pageCount) pageSize = ppsize;
            if (status == "dev")
                Query_module();
            else QueryBox_module();
        }

        private void btn_nextpage_Click(object sender, EventArgs e)
        {
            curpageIndex = curpageIndex + 1 > pageCount ? pageCount : curpageIndex + 1;
            if (curpageIndex + 1 > pageCount) pageSize = ppsize;
            if (status == "dev")
                Query_module();
            else QueryBox_module();
        }

        private void btn_backpage_Click(object sender, EventArgs e)
        {
            curpageIndex = pageCount;
            if (curpageIndex + 1 > pageCount) pageSize = ppsize;
            if(status=="dev")
            Query_module();
            else QueryBox_module();
        }

        private void btn_dev_Click(object sender, EventArgs e)
        {
            status = "dev";
            Query_module();
        }

        private void btn_box_Click(object sender, EventArgs e)
        {
            status = "box";
            curpageIndex = 1;
            QueryBox_module();
        }
    }
}
